struct vertex
{
	float4 position : POSITION;
    float4 normal   : NORMAL;
	float4 color0   : COLOR0;
	float3 bounds1  : TEXCOORD0;
	float3 bounds2	: TEXCOORD1;
};

struct fragment
{
	float4 position : POSITION;
	float4 color0   : COLOR0;
};

uniform float4x4 modelViewProj;

uniform float4 light;

//matrices for right lower leg movement
uniform float4x4 toOrigin0;
uniform float4x4 rot0;
uniform float4x4 fromOrigin0;

//matrices for left lower leg movement
uniform float4x4 toOrigin1;
uniform float4x4 rot1;
uniform float4x4 fromOrigin1;

//matrices for right leg movement

uniform float4x4 toOrigin2;
uniform float4x4 rot2;
uniform float4x4 fromOrigin2;

//matrices for left leg movement

uniform float4x4 toOrigin3;
uniform float4x4 rot3;
uniform float4x4 fromOrigin3;

fragment main(vertex IN)
{
	fragment OUT;
	int leftLeg = 1;
	int rightLeg = 1;
	int leftLowerLeg = 1;
	int rightLowerLeg = 1;
	
	
	if(IN.bounds2[0]) {
		leftLeg = 1;
		
	}
	else {
		rot3[1][1] =  rot3[2][2] = 1;
		rot3[2][1] = rot3[1][2] = 0;
	}
	if (IN.bounds1[2]) {
		rightLeg = 1;
		
	}
	else {
		rot2[1][1] =  rot2[2][2] = 1;
		rot2[2][1] = rot2[1][2] = 0;
	}
	if(IN.bounds1[1]) {
		leftLowerLeg = 1;
	}
	else {
		rot1[1][1] =  rot1[2][2] = 1;
		rot1[2][1] = rot1[1][2] = 0;
	}
	if (IN.bounds1[0]) {
		rightLowerLeg = 1;
	}
	else {
		rot0[1][1] =  rot0[2][2] = 1;
		rot0[2][1] = rot0[1][2] = 0;
	}
	
	
	
	float4x4 matrix = modelViewProj;
	
	float4 position = {0.0, 0.0, 0.0, 1.0};
	float4 normal = {0.0, 0.0, 0.0, 1.0};
	
	//IN.bounds[0] = IN.bounds[1] = IN.bounds[2] = IN.bounds[3] = 1;
	
	//if(IN.bounds1[2] == 1) {
		matrix = mul(matrix,fromOrigin2);
		matrix = mul(matrix,rot2);
		matrix = mul(matrix,toOrigin2);
	//}
	
	//else if (IN.bounds2[0] == 1) {
		matrix = mul(matrix,fromOrigin3);
		matrix = mul(matrix,rot3);
		matrix = mul(matrix,toOrigin3);
	//}
	
	//if (IN.bounds1[0] == 1) {
		matrix = mul(matrix,fromOrigin0);
		matrix = mul(matrix,rot0);
		matrix = mul(matrix,toOrigin0);
	//} 
	//else if (IN.bounds1[1] == 1) {
		matrix = mul(matrix,fromOrigin1);
		matrix = mul(matrix,rot1);
		matrix = mul(matrix,toOrigin1);
	//}
	
	
	//IN.position = mul(matrix,IN.position);	
	
	//matrix = modelViewProj;
	
	
	
	
	
	OUT.position = mul(matrix,IN.position);	
	OUT.color0 = IN.color0*dot(light, IN.normal);

	return OUT;
}

technique twist {
  pass {
    VertexProgram = compile arbvp1 main();
  }
}